package org.example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SQLKeywordFinder {

    private static final Pattern CLASS_PATTERN = Pattern.compile("class\\s+(\\w+)");

    public static void findSQLKeywordsInFile(File file) throws IOException {
        String className = null;
        int lineNumber = 0;

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lineNumber++;

                // Try to find the class name in the current line
                if (className == null) {
                    Matcher classMatcher = CLASS_PATTERN.matcher(line);
                    if (classMatcher.find()) {
                        className = classMatcher.group(1);
                    }
                }

                // Check for SQL keywords in the current line
                for (String keyword : SQLKeywords.SQL_KEYWORDS) {
                    if (line.toUpperCase().contains(keyword)) {
                        System.out.println("Found SQL keyword '" + keyword + "' in file: " + file.getName() +
                                ", class: " + className + ", line: " + lineNumber);
                    }
                }
            }
        }
    }
}